package 哈希表;

// 表示链表,存放数据
public class EmpLikedList {
    private Emp head;// 头指针,指向当前链表的第一个雇员

    // 添加雇员
    // 假定id自增长,直接尾增
    public void add(Emp emp){
        if(head == null){
            head = emp;
            return;
        }
        Emp cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = emp;
    }

    // 遍历链表的雇员信息
    public void list(int count){
        if(head == null){
            System.out.println("第"+count+"条链表为空");
            return;
        }
        System.out.println("第"+count+"条链表的信息为");
        Emp cur = head;
        while(cur != null){
            if(cur.next == null){
                System.out.printf("(id = %d name = %s)\n",cur.id,cur.name);
                return;
            }
            System.out.printf("(id = %d name = %s)=>",cur.id,cur.name);
            cur = cur.next;
        }
    }

    // 通过id查找对应的雇员
    public Emp findEmp(int id){
        Emp cur = head;
        while(true){
            if(cur == null){
                System.out.println("雇员不存在");
                return null;
            }
            if(cur.id == id){
                return cur;
            }
            cur = cur.next;
        }
    }
}
